package com.mai4j.maiadmin.controller.api.system.localisation;

import com.mai4j.maiadmin.misc.StructMapper;
import com.mai4j.maiadmin.model.RespCode;
import com.mai4j.maiadmin.model.dto.LocationDTO;
import com.mai4j.maiadmin.model.dto.PageDTO;
import com.mai4j.maiadmin.model.http.LocationDeleteReq;
import com.mai4j.maiadmin.model.http.LocationGetReq;
import com.mai4j.maiadmin.model.http.LocationSaveOrUpdateReq;
import com.mai4j.maiadmin.model.http.datatables.Datatable;
import com.mai4j.maiadmin.model.http.datatables.response.DatatableResp;
import com.mai4j.maiadmin.model.http.response.LocationGetResp;
import com.mai4j.maiadmin.model.http.response.LocationSaveOrUpdateResp;
import com.mai4j.maiadmin.model.http.response.SimpleResp;
import com.mai4j.maiadmin.service.LocationService;
import com.mai4j.maiadmin.support.I18nProvider;
import com.mai4j.maiadmin.support.security.aop.annotation.RequiresAuthentication;
import com.mai4j.maiadmin.support.web.binding.ReqParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
@RequiresAuthentication
@RestController
@RequestMapping("/api/system/localisation/location")
public class ApiLocationController {

    private final LocationService locationService;

    public ApiLocationController(LocationService locationService) {
        this.locationService = locationService;
    }

    /**
     * 分页
     *
     * @param datatable datatable
     */
    @PostMapping("/datatable")
    public ResponseEntity<DatatableResp> datatable(@ReqParam Datatable datatable) {
        PageDTO<LocationDTO> pageDTO = locationService.paginate(datatable);
        return ResponseEntity.ok(new DatatableResp()
                .setDraw(datatable.getDraw())
                .setRecordsFiltered(pageDTO.getTotalRow())
                .setRecordsTotal(pageDTO.getTotalRow())
                .setData(pageDTO.getList()
                        .stream()
                        .map(StructMapper.INSTANCE::locationDTOToLocationVO)
                        .collect(Collectors.toList())));
    }

    @PostMapping("/get")
    public ResponseEntity<LocationGetResp> get(@RequestBody @Valid LocationGetReq locationGetReq) {
        LocationDTO locationDTO = locationService.get(Long.parseLong(locationGetReq.getLocationId()));
        return ResponseEntity.ok(new LocationGetResp()
                .setLocation(StructMapper.INSTANCE.locationDTOToLocationVO(locationDTO))
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    @PostMapping("/delete")
    public ResponseEntity<SimpleResp> delete(@RequestBody @Validated LocationDeleteReq locationDeleteReq) {
        locationService.delete(Long.parseLong(locationDeleteReq.getLocationId()));
        return ResponseEntity.ok(new SimpleResp()
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    @PostMapping("/saveOrUpdate")
    public ResponseEntity<LocationSaveOrUpdateResp> saveOrUpdate(@RequestBody @Validated LocationSaveOrUpdateReq locationSaveOrUpdateReq) {
        LocationDTO locationDTO = locationService.saveOrUpdate(StructMapper.INSTANCE.locationSaveOrUpdateReqToLocationDTO(locationSaveOrUpdateReq));
        return ResponseEntity.ok(new LocationSaveOrUpdateResp()
                .setLocationId(String.valueOf(locationDTO.getLocationId()))
                .setCode(RespCode.OK.getCode())
                .setMessage(
                        I18nProvider.get(
                                Objects.equals(String.valueOf(locationDTO.getLocationId()), locationSaveOrUpdateReq.getLocationId())
                                        ? "text.edit.successfully"
                                        : "text.save.successfully")));
    }
}
